package main

import (
	"go_base/logger"

	"github.com/gin-gonic/gin"
	"github.com/go-basic/uuid"
)

func main() {
	r := gin.Default()

	r.Use(Middleware())
	pingFunc := func(c *gin.Context) {
		logger.Info("哈哈哈哈哈哈哈")
		c.JSON(200, gin.H{
			"message": "pong",
		})
		logger.Info("111111111")
	}
	r.GET("/ping", pingFunc)
	r.Run() // listen and serve on 0.0.0.0:8080
}

func Middleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		r := c.Request
		w := c.Writer
		traceID := r.FormValue("reqid")
		if traceID == "" {
			uid := uuid.New()
			r.Form.Set("reqid", uid)
			traceID = uid
		}
		logger.AddHook(logger.NewTraceIdHook(traceID))

		w.Header().Set("X-Trace-Id", traceID)

		c.Next()
	}

}
